package com.bobi.tfca.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.databinding.ItemMemberAddLayoutBinding
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.mainnavigation.message.groupcreation.GroupCreationFragment
import com.bumptech.glide.Glide

class GroupMemAddAdapter(
    val fragment: Fragment,
    var memberResponseList: ArrayList<MemberResponse>
) :
    RecyclerView.Adapter<GroupMemAddAdapter.MemberViewHolder>(){

    private var mOnItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener{
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }

    inner class MemberViewHolder(private val binding: ItemMemberAddLayoutBinding) : RecyclerView.ViewHolder(binding.root)  {

        fun bind(position: Int) {
            val member = memberResponseList[position]
            binding.member = member
            val url = member.picture
            if (url != null && url.isNotBlank() && (!"null".equals(url))) {
                Glide.with(fragment).load(url).placeholder(R.drawable.default_avatar)
                    .into(binding.memberHead)
            }
            binding.ivRemoveButton.setOnClickListener {
                mOnItemClickListener?.onItemClick(position)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MemberViewHolder {
        val itemView = ItemMemberAddLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = MemberViewHolder(itemView)
        return viewHolder
    }

    override fun getItemCount(): Int = memberResponseList.size

    override fun onBindViewHolder(holder: MemberViewHolder, position: Int) {
        with(holder) {
            bind(position)
        }
    }
}